package com.jvup.gameshow.view.adapter.pager;

import android.view.View;
import android.view.ViewGroup;

import androidx.viewpager.widget.PagerAdapter;

import com.jvup.gameshow.model.viewModel.impl.PlayerShowViewModel.PlayerShowZoneViewModel;

import java.util.ArrayList;

public class PlayerShowPagerAdapter extends PagerAdapter {

    private final ArrayList<PlayerShowZoneViewModel> zoneViewModelList;

    public PlayerShowPagerAdapter(ArrayList<PlayerShowZoneViewModel> zoneViewModelList) {
        this.zoneViewModelList = zoneViewModelList;
    }

    /**
     * 当TabLayout与ViewPager绑定的时候能够绑定Tab标签的标题
     * @param position
     * @return
     */
    @Override
    public CharSequence getPageTitle(int position) {
        return zoneViewModelList.get(position).title();
    }

    @Override
    public int getCount() {
        return zoneViewModelList.size();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View root = zoneViewModelList.get(position).getRoot();
        container.removeView(root);//删除页卡
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {  //这个方法用来实例化页卡
        View root = zoneViewModelList.get(position).getRoot();
        container.addView(root, 0);//添加页卡
        return root;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;//官方提示这样写
    }

}
